#include<bits/stdc++.h>
using namespace std;
namespace fastio
{
	constexpr int S1=1<<20;
	char buf1[S1],*l1,*r1;
	inline char getc()
	{
		return ((l1==r1&&(r1=(l1=buf1)+fread(buf1,1,S1,stdin)),l1!=r1)?*l1++:EOF);
	}
	template<typename T=int>inline T read()
	{
		T x=0,y=1;
		char c=getc();
		for(;c<'0'||c>'9';c=getc())
			if(c=='-')
				y=-1;
		for(;c>='0'&&c<='9';c=getc())
			x=c-'0'+x*10;
		return x*y;
	}
	constexpr int S2=1<<20;
	char buf2[S2],*l2=buf2;
	inline void putc(char c)
	{
		l2==buf2+S2&&(fwrite(buf2,1,S2,stdout),l2=buf2),*l2++=c;
	}
	int st[22];
	template<typename T>inline void write(T x,char end='\n')
	{
		if(x<0)
			putc('-'),x=-x;
		int tp=0;
		do
			st[++tp]=x%10;
		while(x/=10);
		while(tp)
			putc(st[tp--]+'0');
		if(end)
			putc(end);
	}
	struct end
	{
		~end()
		{
			fwrite(buf2,1,l2-buf2,stdout);
		}
	}ender;
}
using fastio::getc;
using fastio::read;
using fastio::putc;
using fastio::write;
constexpr int MN=131100;
int T,n,q,K,a0[MN],a[MN],qv[MN],rq[MN];
vector<int>o[20],f[20],vis[20];
long long ans[MN],tans;
void dfs(int d,int x,int rq)
{
	if(d==K)
	{
		::rq[x]=rq;
		return;
	}
	bool no=o[d][x];
	dfs(d+1,x<<1,(!rq&&!no)?K-d:rq);
	dfs(d+1,x<<1|1,(!rq&&no)?K-d:rq);
}
void fil(int d,int x)
{
	if(vis[d][x])
		return;
	vis[d][x]=1;
	if(d==K)
	{
		tans-=x+1;
		return;
	}
	fil(d+1,x<<1);
	fil(d+1,x<<1|1);
}
inline void up(int d,int x)
{
	int son=x<<1|o[d][x],j=f[d+1][son];
	if(j==-1)
	{
		f[d][x]=-1;
		return;
	}
	if(a[j]>=K-d)
		f[d][x]=j,fil(d+1,son^1);
	else
		f[d][x]=f[d+1][son^1];
}
inline void work()
{
	for(int j=0;j<=K;j++)
		for(int i=0;i<(1<<j);i++)
			f[j][i]=-1,vis[j][i]=0;
	f[K][0]=0;
	ans[0]=tans=1;
	for(int k=1;k<=K;k++)
	{
		long long L=(1<<(k-1))+1,R=(1<<k);
		tans+=(L+R)*(R-L+1)/2;
		bool no=o[K-k][0];
		dfs(K-k+1,1,no?k:0);
		if(!no)
		{
			if(a[f[K-k+1][0]]>=k)
				f[K-k][0]=f[K-k+1][0],fil(K-k+1,1);
			else
				fil(K-k+1,0);
		}
		for(int i=(1<<(k-1));i<(1<<k);i++)
		{
			f[K][i]=i;
			if(a[i]<rq[i])
				fil(K,i);
			for(int x=i>>1,d=K-1;d>=K-k;x>>=1,d--)
			{
				int pre=f[d][x];
				up(d,x);
				if(f[d][x]==pre)
					break;
			}
			ans[i]=tans;
		}
	}
	long long res=0;
	for(int i=1;i<=q;i++)
		res^=i*ans[qv[i]];
	printf("%lld\n",res);
}
int main()
{
	freopen("arena.in","r",stdin);
	freopen("arena.out","w",stdout);
	n=read(),q=read(),K=__lg(n-1)+1;
	for(int k=0;k<=K;k++)
	{
		o[k].resize(1<<k);
		f[k].resize(1<<k);
		vis[k].resize(1<<k);
	}
	for(int i=0;i<n;i++)
		a0[i]=read();
	for(int i=1;i<=q;i++)
		qv[i]=read()-1;
	for(int k=K-1;~k;k--)
	{
		char c=getc();
		while(c!='0'&&c!='1')
			c=getc();
		for(int i=0;i<(1<<k);i++)
			o[k][i]=c-'0',c=getc();
	}
	T=read();
	while(T--)
	{
		int X[4];
		for(int i=0;i<4;i++)
			X[i]=read();
		for(int i=0;i<n;i++)
			a[i]=a0[i]^X[(i+1)%4];
		work();
	}
	return 0;
}